package it.uniba.di.mas.jpnde.behaviour;

import it.uniba.di.mas.jpnde.agent.JPNDEConsole;
import it.uniba.di.mas.jpnde.console.IConsoleBackEnd;
import it.uniba.di.mas.jpnde.console.IConsoleUI;
import it.uniba.di.mas.jpnde.core.PN;
import jade.core.behaviours.SequentialBehaviour;


public class JPNDEConSeqBehaviour extends SequentialBehaviour{
	private IConsoleBackEnd consoleBE=null;
	private IConsoleUI console=null;
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public JPNDEConSeqBehaviour(JPNDEConsole consoleBE,IConsoleUI console){
		super(consoleBE);
		this.consoleBE=consoleBE;
		this.console=console;
	}
	
	public int onEnd() {
		int ret=super.onEnd();
		
		System.out.println("JPNDEConSeqBehaviour.onEnd :"+myAgent.getAID().getName());
		
		for(int i=0;i<consoleBE.getManagedPNs().size();i++)
			{
			PN pn=(PN)consoleBE.getManagedPNs().get(i);
			if (pn.getState().equals(PN.STATE_STARTING))
				if (pn.getAgentId()!=null&&
					!pn.getAgentId().equals("")){
					pn.setState(PN.STATE_RUNNING);
					console.appendLogMessage("PN<"+pn.getId()+"> started !");
					}
				else{
					pn.setState(PN.STATE_ERROR);
					console.appendLogMessage("PN<"+pn.getId()+"> not started !");
					}
			}
		console.managedPNsChange();
		consoleBE.setCurrentBehaviour(new JPNDEConsoleBehaviour(consoleBE,console));
		myAgent.addBehaviour(consoleBE.getCurrentBehaviour());
		return ret;
	}
}
